﻿using CoreShop.Model;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Asn1.Ocsp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CoreShop.Library.Units
{
    public class HttpHelperAsync
    {
        // 发送POST请求并处理响应
        public static async Task<string> PostAsync(string url, string body)
        {
            // 创建要发送的JSON内容
            var jsonContent = new StringContent(body, Encoding.UTF8, "application/json");
            HttpClient httpClient = new HttpClient();

            // 异步发送POST请求
            using (HttpResponseMessage response = await httpClient.PostAsync(url, jsonContent))
            {
                // 确保响应成功
                response.EnsureSuccessStatusCode();

                // 异步读取响应内容
                using (HttpContent content = response.Content)
                {
                    // 以字符串形式读取内容
                    return await content.ReadAsStringAsync();
                }
            }
        }


        // 发送POST请求并处理响应
        public static async Task<string> PostDataAsync(string url, string body)
        {           
            // 创建要发送的JSON内容
            var jsonContent = new StringContent(body, Encoding.UTF8, "application/json");
            jsonContent.Headers.Add("X-Authorization", "true");
            jsonContent.Headers.Add("X-AuthorizationType", "POS_ACCOUNT_PASSWORD_LOGIN");
            HttpClient httpClient = new HttpClient();

            // 异步发送POST请求
            using (HttpResponseMessage response = await httpClient.PostAsync(url, jsonContent))
            {
                // 确保响应成功
                response.EnsureSuccessStatusCode();

                // 异步读取响应内容
                using (HttpContent content = response.Content)
                {
                    // 以字符串形式读取内容
                    return await content.ReadAsStringAsync();
                }
            }
        }


        // 发送POST请求并处理响应
        public static async Task<ReturnModel> PostDataAsync(string urlStr, string param = "", string body = "{}", double timeout = 60.0, string authorizationType = "", sysuser sysUser = null)
        {
            ReturnModel returnModel = new ReturnModel() { Status = ReturnStatus.Success };

            string url = urlStr + param;
            // 创建要发送的JSON内容
            var jsonContent = new StringContent(body, Encoding.UTF8, "application/json" );

            if (!string.IsNullOrEmpty(authorizationType))
            {
                jsonContent.Headers.Add("X-Authorization", "true");
                jsonContent.Headers.Add("X-AuthorizationType", authorizationType);
            }
            else if (sysUser != null)
            {
                jsonContent.Headers.Add("X-Authorization", "true");
                jsonContent.Headers.Add("X-AuthorizationType", "MD5 Pos");
                jsonContent.Headers.Add("tenantId", sysUser.corpId.ToString());
            }
            HttpClient httpClient = new HttpClient();
            if (sysUser != null)
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", sysUser.ccessToken);
            }
            
            // 异步发送POST请求
            using (HttpResponseMessage response = await httpClient.PostAsync(url, jsonContent))
            {
                // 确保响应成功
                response.EnsureSuccessStatusCode();

                // 异步读取响应内容
                using (HttpContent content = response.Content)
                {
                    // 以字符串形式读取内容
                    returnModel.MessageOrString = await content.ReadAsStringAsync(); 
                }
            }
            return returnModel;
        }

        // 发送GET请求并处理响应
        public static async Task<ReturnModel> GetDataAsync(string urlStr, string param, double timeout = 30.0, sysuser sysUser = null)
        {
            ReturnModel returnModel = new ReturnModel() { Status = ReturnStatus.Success };
            string url = urlStr + param;

            HttpClient httpClient = new HttpClient();
            httpClient.Timeout = TimeSpan.FromSeconds(60);

            if (sysUser == null)
            {
                httpClient.DefaultRequestHeaders.Add("X-Authorization", "true");
                httpClient.DefaultRequestHeaders.Add("X-AuthorizationType", "MD5 Pos");
                httpClient.DefaultRequestHeaders.Add("tenantId", sysUser.corpId.ToString());
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "");
            }
            else
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", sysUser.ccessToken);
            }

            // 异步发送GET请求
            using (HttpResponseMessage response = await httpClient.GetAsync(url))
            {
                // 确保响应成功
                response.EnsureSuccessStatusCode();

                // 异步读取响应内容
                using (HttpContent content = response.Content)
                {
                    // 以字符串形式读取内容
                    returnModel.MessageOrString = await content.ReadAsStringAsync();
                }
            }
            return returnModel;
        }

    }
}
